'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = '<h1>UserController</h1>';
  }
  // 查
  async find() {
    const { ctx } = this;
    const sql = `select * from users;`
    const data = await ctx.app.mysql.query(sql);
    ctx.body = data;
  }
  // 增
  async add() {
    const { ctx } = this;
    const { username, password, realname } = ctx.request.body;
    const sql = `insert into users (username, password, realname) values ('${username}', '${ctx.helper.getPassword(password)}', '${realname}');`
    const data = await ctx.app.mysql.query(sql);
    ctx.body = data;
  }
  // 改
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const { username, password, realname } = ctx.request.body;
    const sql = `update users set username='${username}', password='${password}', realname='${realname}' where id = ${id};`
    const data = await ctx.app.mysql.query(sql);
    ctx.body = data;
  }
  // 删
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const sql = `delete from users where id = ${id};`
    const data = await ctx.app.mysql.query(sql);
    ctx.body = data;
  }
}

module.exports = UserController;
